import request from '@/utils/request'

// 获取学生列表
export function getStudentList(params) {
  return request({
    url: '/api/user/paging',
    method: 'get',
    params: {
      userType: 1,
      ...params,
    },
  })
}

// 获取教师列表
export function getTeacherList(params) {
  return request({
    url: '/api/user/paging',
    method: 'get',
    params: {
      userType: 2,
      ...params,
    },
  })
}

// 编辑用户信息
export function editUser(data) {
  return request({
    url: '/api/user/info',
    method: 'put',
    data,
  })
}

// 添加用户
export function addUser(data) {
  return request({
    url: '/api/user',
    method: 'post',
    data,
  })
}

// 获取班级信息
export function getGradeList() {
  return request({
    url: '/api/grades/paging',
    method: 'get',
  })
}

// 删除用户
export function deleteUser(params) {
  return request({
    url: `/api/user`,
    method: 'delete',
    params,
  })
}

// 批量删除用户
export function batchDeleteUser(ids, userType = 1) {
  // 使用URLSearchParams构建参数
  const searchParams = new URLSearchParams()

  // 添加userType参数
  searchParams.append('userType', userType)

  // 为每个id添加一个ids参数
  ids.forEach((id) => {
    searchParams.append('ids', id)
  })

  // 将URLSearchParams转换为字符串
  const queryString = searchParams.toString()

  return request({
    url: `/api/user`,
    method: 'delete',
    params: {},
    paramsSerializer: () => queryString,
  })
}
